<?php

/**
 * uniBoard Autoloader Function
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

/**
 * __autoload function
 *
 * @param string $class_name
 */
function __autoload( $class_name){
	
	try
	{
		//Determine file type using first 3 characters of class name
		switch ( substr( $class_name, 0, 3))
		{
			// Include Action
			case 'act':
				if (file_exists( ACTIONS_GLOBAL_PATH . $class_name . PHP_EXT ))
				{
					require_once( ACTIONS_GLOBAL_PATH . $class_name . PHP_EXT );
				}
				else if ( IN_ADMIN && file_exists( ACTIONS_ADMIN_PATH . $class_name . PHP_EXT ))
				{
					require_once( ACTIONS_ADMIN_PATH . $class_name . PHP_EXT );
				}
				else if ( !IN_ADMIN && file_exists( ACTIONS_PUBLIC_PATH . $class_name . PHP_EXT ))
				{
					require_once( ACTIONS_PUBLIC_PATH . $class_name . PHP_EXT );
				}
				else
				{
					throw new exc_critical( 'FILE', $class_name . ' declaration not found.', 'Searched action file was not found' );
				}
			break;
			
			// Include API
			case 'api':
				if ( file_exists( APIS_PATH . $class_name . PHP_EXT ))
				{
					require_once( APIS_PATH . $class_name . PHP_EXT );
				}
				else
				{
					throw new exc_critical( 'FILE', $class_name . ' declaration not found.', 'Searched file was not found in ' . APIS_PATH );
				}
			break;
			
			// Include Application
			case 'app':
				if ( file_exists( APPLICATIONS_PATH . $class_name . PHP_EXT ))
				{
					require_once( APPLICATIONS_PATH . $class_name . PHP_EXT );
				}
				else
				{
					throw new exc_critical( 'FILE', $class_name . ' declaration not found.', 'Searched file was not found in ' . APPLICATIONS_PATH );
				}
			break;
				
			// Include Class
			case 'cla':
				if ( file_exists( CLASSES_PATH . $class_name . PHP_EXT ))
				{
					require_once( CLASSES_PATH . $class_name . PHP_EXT );
				}
				else
				{
					throw new exc_critical( 'FILE', $class_name . ' declaration not found.', 'Searched file was not found in ' . CLASSES_PATH );
				}			
			break;	
				
			// Include Exception
			case 'exc':
				if ( file_exists( EXCEPTIONS_PATH . $class_name . PHP_EXT ))
				{
					require_once( EXCEPTIONS_PATH . $class_name . PHP_EXT );
				}
				else
				{
					throw new exc_critical( 'FILE', $class_name . ' declaration not found.', 'Searched file was not found in ' . EXCEPTIONS_PATH );
				}			
			break;	
			
			// Include Template
			case 'tpl':
				// Admin template?
				if ( IN_ADMIN)
				{
					if ( file_exists( ADMIN_TEMPLATES_PATH . $class_name . PHP_EXT ))
					{
						require_once( ADMIN_TEMPLATES_PATH . $class_name . PHP_EXT );
					}
					else
					{
						throw new exc_critical( 'STYLE', 'Compilet templates ' .$class_name . ' not found.', 'Searched file was not found in ' . ADMIN_TEMPLATES_PATH );
					}
				}
				else
				{				
					// Now check template existence
					if ( file_exists( STYLES_DATA_PATH . 'style_' . appCore::$style . '/' . $class_name . PHP_EXT ))
					{
						require_once( STYLES_DATA_PATH . 'style_' . appCore::$style . '/' . $class_name . PHP_EXT );
					}
					else
					{
						throw new exc_critical( 'STYLE', 'Compiled templates ' . $class_name . ' not found.', 'Searched file was not found in ' . STYLES_DATA_PATH . 'style_' . appCore::$style . '/' . $class_name . PHP_EXT );
					}
				}			
			break;
		}
		
		// Class declared?
		if ( !class_exists( $class_name ))
		{
			throw new exc_critical( 'FILE', $class_name . ' declaration not found in loaded file.', 'Searched class declaration was not found in loaded file.' );
		}				
	}
	catch ( exc_critical $exception)
	{	
		// Stop software execution with this exception
		die( $exception);
	}
}